package lcr;

import java.util.HashMap;
import java.util.Map;

public class Test0065 {
    public static void main(String[] args) {
//        String[] words = {"time", "me", "bell"};
        String[] words = {"me", "time"};
//        String[] words = {"t"};
        Solution0065_1 solution = new Solution0065_1();
        System.out.println(solution.minimumLengthEncoding(words));
    }
}

class Solution0065_1 {
    public int minimumLengthEncoding(String[] words) {
        Trie trie = new Trie();
        HashMap<Trie, Integer> map = new HashMap<>();
        for (int i = 0; i < words.length; i++) {
            char[] chars = words[i].toCharArray();
            Trie cur= trie;
            for (int j = chars.length - 1; j >= 0; j--) {
                cur=cur.get(chars[j]);
            }
            map.put(cur, i);
        }

        int ans = 0;
        for (Map.Entry<Trie, Integer> ent : map.entrySet()){
            if (ent.getKey().count==0) {
                ans+= words[ent.getValue()].length() + 1;
            }
        }

        return ans;
    }

    class Trie {
        int count=0;
        Trie[] children;

        public Trie get(char c){
            if (children==null) children=new Trie[26];
            Trie child = children[c - 'a'];
            if (child==null){
                child = new Trie();
                children[c-'a'] = child;
                count++;
            }
            return child;
        }
    }
}
